package Gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JPanel;

public class AboutPanel extends Frame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5320215471269705069L;
	private static AboutPanel ref;
	TextArea textarea;

	private AboutPanel(String filename) {
		super();
		// create TextArea
		textarea = new TextArea("", 24, 80);
		textarea.setFont(new Font("MonoSpaced", Font.PLAIN, 16));
		textarea.setEditable(false);
		this.add("Center", textarea);

		// create bottom panel
		// Panel p = new Panel();
		JPanel p = new JPanel();
		p.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));
		// this.add(p, "South");
		GameFrame.getInstance().add(p);

		JButton ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});

		add(ok, BorderLayout.SOUTH);
		loadFile(filename);
		this.pack();
		setLocationRelativeTo(GameFrame.getInstance());
		
		setVisible(true);
	}

	private void loadFile(String filename) {
		if ((filename == null))// ||(filename.length() == 0))
			return;
		URL file = null;
		try
		{
			file = new URL(filename);
		} catch (MalformedURLException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//File f;
		Scanner in = null;
		// Read and display the file contents. Since we're reading text, we
		// use a FileReader instead of a FileInputStream.
		
			//f = new File(filename); // Create a file object
			
			try
			{
				in = new Scanner(file.openStream());
				textarea.setText(""); // Clear the text area
				while(in.hasNextLine()){
					textarea.append(in.nextLine() + "\n");
				}
				

			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 // And a char stream to read it
			//char[] buffer = new char[4096]; // Read 4K characters at a time
			//int len; // How many chars read each time
			
			//while ((len = in.read(buffer)) != -1) { // Read a batch of chars
			//	String s = new String(buffer, 0, len); // Convert to a string
			
			//this.setTitle("FileViewer: " + filename); // Set the window title
			//textarea.setCaretPosition(0); // Go to start of file
		
		// Display messages if something goes wrong
		
		// Always be sure to close the input stream!
	
			if (in != null)
				in.close();
	}

	public static AboutPanel getInstance() {
		if (ref == null)
			ref = new AboutPanel("http://www.stevenbankhead.com/GSP_Project/TextFiles/gameAbout.txt");
	
		return ref;
	}
}